package cn.dylan.xhr;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class XhrConsumer {
    public static void main(String[] args) throws Exception {
        // 创建consumer对象
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("xhr_consumer_group");
        // 设置nameAddress
        consumer.setNamesrvAddr("localhost:9876");
        // 订阅topic
        consumer.subscribe("xhr","hello");
        //
        consumer.registerMessageListener((MessageListenerConcurrently) (list, consumeConcurrentlyContext) -> {
            for (MessageExt messageExt : list) {
                System.out.println(new String(messageExt.getBody(), StandardCharsets.UTF_8));
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        });

        consumer.start();
    }
}
